package pl.pages;

import java.util.Date;

import org.apache.tapestry.annotations.InitialValue;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.apache.tapestry.form.StringPropertySelectionModel;
import org.apache.tapestry.html.BasePage;

import pl.model.Celebrity;
import pl.model.DataSource;

public abstract class AddCelebrity extends BasePage {

	@InjectState("dataSource")
	public abstract DataSource getDataSource();
	
	@InitialValue("literal:F")
	public abstract String getGender();
	
	public abstract String getFirstName();

	public abstract String getLastName();
	
	public abstract String getOccupation();
	
	public abstract Date getDateOfBirth();
	
	public abstract Date getDateOfBirth2();
	
	public IPropertySelectionModel getOccupationsModel() {

		String[] occupations = {"Actor/Actress",
				"Wine-maker", "Programmer"};
		return new StringPropertySelectionModel(occupations);

	}  
	
	public String onAddCelebrity() {

		   DataSource ds = getDataSource();
		   Celebrity celebrity = new Celebrity();
		   celebrity.setFirstName(getFirstName());
		   celebrity.setLastName(getLastName());
		   celebrity.setDateOfBirth(getDateOfBirth());
		   celebrity.setDateOfBirth2(getDateOfBirth2());
		   celebrity.setId(ds.getNewId());

		   String occupation = getOccupation();

		   if ("Actor/Actress".equals(occupation)) {
		     if ("F".equals(getGender())) {
		       occupation = "Actress";
		     }
		     else {
		       occupation = "Actor";
		     }
		   }

		   celebrity.setOccupation(occupation);
		   ds.addCelebrity(celebrity);
		   return "CelebritiesList";
		}
	
}
